package com.guli.cart.vo;

import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

/**
 * @author WZH
 * @title: CartVo
 * @projectName gulimall-java
 * @description: 整个购物车存放的商品信息
 * @desc 需要计算的属性需要重写get方法，保证每次获取属性都会进行计算
 * @date 2022/1/19
 */
public class CartVo {
    private List<CartItemVo> items; //所有商品信息

    private Integer countNum; //商品数量

    private Integer countType; //商品类型数

    private BigDecimal totalAmount; //商品总价

    private BigDecimal reduce=BigDecimal.ZERO; //减免价格默认为0

    /**
     * 应付价格
     */
    private BigDecimal payAmount=BigDecimal.ZERO;

    public List<CartItemVo> getItems() {
        return items;
    }

    public void setItems(List<CartItemVo> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        int num = 0;
        for (CartItemVo item : this.items) {
            num += item.getCount();
        }
        return num;
    }

    public Integer getCountType() {
        int num = 0;
        for (CartItemVo item : this.items) {
            num += 1;
        }
        return num;
    }
    //总价
    public BigDecimal getTotalAmount() {
        BigDecimal zero = BigDecimal.ZERO;
        for (CartItemVo item : this.items) {
            if(item.getCheck()){
                zero = zero.add(item.getTotalPrice());
            }
        }
        return zero;
    }
    //应付总价
    public BigDecimal getPayAmount() {
        return getTotalAmount().subtract(this.reduce);
    }
}
